package com.shell.customer.base.service.impl;

import com.shell.bee.base.utils.StringUtil;
import com.shell.customer.base.service.CusCheckService;
import com.shell.customer.base.service.CusRecService;
import com.shell.customer.base.vo.CusRecVo;
import com.shell.customer.po.CusRec;
import com.shell.customer.register.vo.RegisterVo;
import com.shell.feign.PubUserExtraFeign;
import com.shell.feign.PubUserFeign;
import com.shell.governor.user.po.PubUser;
import com.shell.governor.user.po.PubUserExtra;
import com.shell.governor.user.vo.PubUserVo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service("pubCusCheckService")
public class CusCheckServiceImpl implements CusCheckService {
	@Autowired
	private CusRecService pubCustomerCService;
	@Autowired
	private PubUserFeign pubUserFeign;
	@Autowired
	private PubUserExtraFeign pubUserExtraFeign;

	public CusRec checkCus(String cusName, String cusType, String srcId) {
		CusRec cusRec = this.pubCustomerCService.selectCustomerByNameTypeSrcId(cusName, cusType, srcId);

		return cusRec;
	}

	public CusRec checkFin(String cusName) {
		CusRec cusRec = new CusRec();
		cusRec.setCusName(cusName);
		cusRec.setCusType("FINNER");
		cusRec.setCusState("E");
		cusRec = (CusRec) this.pubCustomerCService.selectOne(cusRec);

		return cusRec;
	}

	public boolean checkCusExist(String cusName) {
		Example example = new Example(CusRec.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("cusName", cusName);

		List<CusRec> cs = this.pubCustomerCService.selectByExample(example);
		boolean result = false;
		if (cs.size() > 0) {
			result = true;
		}
		return result;
	}

	public boolean checkUserByPhoneNo(String phoneNo, String userId) {
		PubUser pubUser = new PubUser();
		pubUser.setPhoneNo(phoneNo);
		List<PubUserVo> users = this.pubUserFeign.selectByEntity(pubUser);
		if ((users != null) && (users.size() > 0)) {
			PubUser user = (PubUser) users.get(0);
			if (user.getUserId().equals(userId)) {
				return true;
			}
			return false;
		}
		return true;
	}

	public String checkCusRegister(RegisterVo registerVo) {
		if (checkCusExist(registerVo.getCusName())) {
			return "������������������";
		}
		boolean flag = checkUserByPhoneNo(registerVo.getContactorPhone(), null);
		if ((StringUtil.notEmpty(registerVo.getPassword())) && (!flag)) {
			return "����������������";
		}
		if ((!StringUtil.notEmpty(registerVo.getPassword())) && (flag)) {
			return "��������������";
		}
		return "";
	}

	public boolean checkInviteCode(String inviteCode) {
		PubUserExtra userExtra = new PubUserExtra();
		userExtra.setInviteCode(inviteCode);
		userExtra = this.pubUserExtraFeign.selectOne(userExtra);
		if (userExtra != null) {
			return true;
		}
		return false;
	}

	public String checkCusForUpdate(CusRecVo cus) {
		CusRec tempCus = null;

		tempCus = this.pubCustomerCService.checkCusName(cus.getCusId(), cus.getCusCode());
		if (tempCus != null) {
			return "��������������" + cus.getCusName() + "����������";
		}
		tempCus = this.pubCustomerCService.checkCustCode(cus.getCusId(), cus.getCusCode());
		if (tempCus != null) {
			return "��������" + cus.getCusCode() + "������������";
		}
		if (!checkUserByPhoneNo(cus.getContactorPhone(), cus.getContactorId())) {
			return "��������������" + cus.getContactorPhone() + "������������";
		}
		return "";
	}
}